// 实现这个项目的构建任务
const { src, dest, series, parallel, watch } = require('gulp')

const del = require('del')
const browserSync = require('browser-sync')

const loadPlugins = require('gulp-load-plugins') // 通过此依赖加载所以安装的依赖

const plugins = loadPlugins() // 输出所有依赖的对象  如果依赖为 gulp-a-b 会自动驼峰命名 plugins.aB

const bs = browserSync.create()

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

// 删除temp
const clean = () => {
  return del(['dist', 'temp'])
}

const style = () => {
  return src('src/assets/styles/*.scss', { base: 'src' }) // 规范输出路径和输入路径一样
    .pipe(plugins.sass({ outputStyle: 'expanded' })) // 代码展开
    .pipe(dest('temp'))
  // .pipe(bs.reload({ stream: true })) // bs.reload:刷新浏览器 用法:watch监听改变后执行当前方法刷新
}

// 脚本编译
const script = () => {
  // 安装@babel/core , @babel/preset-env, @babel/preset-env会将所有es新特性进行转换
  return src('src/assets/scripts/*.js', { base: 'src' })
    .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
    .pipe(dest('temp'))
}

// 模板编译
const page = () => {
  // src下任意子目录
  return src('src/**/*.html', { base: 'src' })
    .pipe(plugins.swig({ data, defaults: { cache: false } })) // data 模板变量, defaults防止模板缓存导致页面不能及时更新
    .pipe(dest('temp'))
}

const image = () => {
  return src('src/assets/images/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('temp'))
}

const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('temp'))
}

const extra = () => {
  return src('public/**', { base: 'public' })
    .pipe(dest('temp'))
}

const serve = () => {
  watch('src/assets/styles/*.scss', style)
  watch('src/assets/scripts/*.js', script)
  watch('src/**/*.html', page)
  // watch('src/assets/images/**', image)
  // watch('src/assets/fonts/**', font)
  // watch('public/**', extra)

  watch([
    'src/assets/images/**',
    'src/assets/fonts/**',
    'public/**'
  ], bs.reload) // 当静态资源更新后刷新浏览器

  bs.init({
    notify: false, // 关闭右上角启动提示
    port: 2080,// 端口
    // open: false,  // 自动打开浏览器
    files: 'temp/**', // 监听temp下所有文件热更新
    server: {
      baseDir: ['temp', 'src', 'public'], // 运行文件地址
      routes: { // 优先于baseDir运行先走routes配置
        '/node_modules': 'node_modules'
      }
    }
  })
}


const useref = () => {
  return src('temp/*.html', { base: 'temp' })
    .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
    // html js css 判断是否以这三个结尾
    .pipe(plugins.if(/\.js$/, plugins.uglify())) // 压缩js
    .pipe(plugins.if(/\.css$/, plugins.cleanCss())) // 压缩css
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({ // 压缩html
      collapseWhitespace: true, // 压缩html中所有折行
      minifyCSS: true, // 压缩html中所有css
      minifyJS: true // 压缩html中所有js
      // 官网: 删除所有注释...
    })))
    .pipe(dest('dist'))
}

const compile = parallel(style, script, page)

// 上线之前执行的任务
const build = series(
  clean,
  parallel(
    series(compile, useref),
    image,
    font,
    extra
  )
)


const dev = series(clean, compile, serve)

module.exports = {
  clean,
  build,
  dev
}